package com.raising.utils;

import com.raising.framework.config.JConfig;
import com.raising.framework.spring.SpringUtils;
import org.apache.shiro.cache.CacheManager;

/**
 * Cache工具类
 * @author GaoYuan
 * 2017-09-26 16:29:53
 */
public class JCacheUtils  extends BaseUtils{

    private static CacheManager cacheManager = SpringUtils.getBean("cacheManager");
    /**系统默认配置*/
    private static final String SYS_CACHE = JConfig.getConfig("raising.default.cache");
    /**消息推送缓存*/
    public static final String COMET_CACHE = JConfig.getConfig("raising.comet.cache");
    /**短信缓存*/
    public static final String SM_CACHE = JConfig.getConfig("raising.shortMessage.cache");
    /**登录缓存*/
    public static final String LOGIN_CACHE = JConfig.getConfig("raising.login.cache");
    /**密码错误记录缓存*/
    public static final String PWD_RETRY_CACHE = JConfig.getConfig("raising.passwordRetry.cache");
    /**敏感词记录缓存*/
    public static final String SWORD_CACHE = JConfig.getConfig("raising.sword.cache");
    
    /**
     * 获取SYS_CACHE缓存
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        return get(SYS_CACHE, key);
    }

    /**
     * 写入SYS_CACHE缓存
     *
     * @param key
     * @return
     */
    public static void put(String key, Object value) {
        put(SYS_CACHE, key, value);
    }

    /**
     * 从SYS_CACHE缓存中移除
     *
     * @param key
     * @return
     */
    public static void remove(String key) {
        remove(SYS_CACHE, key);
    }

    /**
     * 获取缓存
     *
     * @param cacheName
     * @param key
     * @return
     */
    public static Object get(String cacheName, String key) {
        return getCache(cacheName).get(key);
    }

    /**
     * 写入缓存
     *
     * @param cacheName
     * @param key
     * @param value
     */
    public static void put(String cacheName, String key, Object value) {
        getCache(cacheName).put(key,value);
    }

    /**
     * 从缓存中移除
     *
     * @param cacheName
     * @param key
     */
    public static void remove(String cacheName, String key) {
        getCache(cacheName).remove(key);
    }

    /**
     * 获得一个Cache
     * @param cacheName
     * @return
     */
	private static org.apache.shiro.cache.Cache<Object,Object> getCache(String cacheName) {
        return cacheManager.getCache(cacheName);
    }

}
